﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FrogEngine
{
	public abstract class GameEntityBase : IGameEntity
	{
		public GameEntityBase(IEngine engine)
		{
			this.engine = engine;
			IsAlive = true;
			services = new HashSet<IGameEntityService>();
			engine.Entities.Add(this);
		}

		protected IEngine engine;

		private bool isAlive;
		public bool IsAlive
		{
			get { return isAlive; }
			set
			{
				isAlive = value;
				if (value == false)
					onDeath();
			}
		}

		private void onDeath()
		{
			engine.Entities.Remove(this);
			if (IDied != null)
				IDied(this, new EventArgs());
		}

		public event EventHandler IDied;

		protected HashSet<IGameEntityService> services;
		public ISet<IGameEntityService> Services
		{
			get { return services; }
		}

		public T GetService<T>()
		{
			return (T)services.Single(a => a is T);
		}
	}
}
